﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;

namespace DataAccess
{
    public class ProductsRepository : ConnectionClass
    {
        public ProductsRepository() : base() { }

        public Product GetProduct(int productID)
        {
            return entities.Products.SingleOrDefault(p => p.ID == productID);
        }

        public IEnumerable<Product> GetProducts()
        {
            return entities.Products.AsEnumerable();
        }

        public IEnumerable<Product> GetProductsByName(string name)
        {
            return (from p in entities.Products
                    where p.Name.Contains(name)
                    select p);
        }

        public IEnumerable<Product> GetProductNameInUserFaults(string username)
        {
            return (from uf in entities.UserFaults
                       join p in entities.Products on uf.Product_FK equals p.ID
                       where uf.Username_FK == username                      
                      
                    select p
                  );

        }

        public IEnumerable<Product> GetProductByCategory(int CategoryID)
        {
            return (from p in entities.Products
                    where p.Category_FK == CategoryID
                    select p);
        }

        public IEnumerable<Product> GetProductsByDateListed()
        {
            return (from p in entities.Products
                    select p).OrderBy(p => p.DateListed);
        }

        public IEnumerable<Product> GetProductsByPriceAsc()
        {
            return (from p in entities.Products
                    select p).OrderBy(p => p.Price);
        }

        public IEnumerable<Product> GetProductsByPriceDesc()
        {
            return (from p in entities.Products
                    select p).OrderByDescending(p => p.Price);
        }

        public void UpdateProduct(Product productToUpdate)
        {
            Product originalProduct = GetProduct(productToUpdate.ID); //getting the orignal product
            entities.Products.Attach(originalProduct);//to indicate that we're going to update this record
            entities.Products.ApplyCurrentValues(productToUpdate);
            entities.SaveChanges();
        }

        //public IQueryable<ProductReportView> GetProductsForReport(DateTime fromDate, DateTime toDate)
        //{
        //    return (from p in Entity.Products
        //            from od in Entity.OrderDetails
        //            join o in Entity.Orders on od.OrderID_FK equals o.ID
        //            where od.ProductID_FK == p.ID && o.Date >= fromDate && o.Date <= toDate
        //            group od by p into productGroups
        //            select new Common.Views.ProductReportView
        //            {
        //                Product = productGroups.Key,
        //                ProductQuantity = productGroups.Sum(x => x.Quantity)
        //            }
        //            ).OrderByDescending(x => x.ProductQuantity).Distinct().Take(10);
        //}



    }
}
